﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form4 : Form
    {

        List<string> list = new List<string>();

        string str_1;
        string str_2;
        string str_3;
        string str_4;

        public Form4()
        {
            InitializeComponent();
        }
        //获取填写内容
        //三角形
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            str_1 = textBox1.Text;
        }
        //圆
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            str_2 = textBox2.Text;
        }
        //正方形
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            str_3 = textBox3.Text;
        }
        //长方形
        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            str_4 = textBox4.Text;
        }

        //判断圆是否合法
        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                double r = double.Parse(str_2);
                Circular circular = new Circular(r);
                if (circular.isReasonable())
                {
                    //合法
                    MessageBox.Show("合法");
                }
                else
                {
                    MessageBox.Show("不合法");
                }
            }
            catch
            {
                MessageBox.Show("输入错误，请您检查您输入的内容。");
            }
        }
        //计算圆的面积
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                double r = double.Parse(str_2);
                if (str_2 != null)
                {
                    if (r >= 0)
                    {
                        Circular circular = new Circular(r);
                        MessageBox.Show("圆的面积：" + circular.CalculatedArea().ToString());
                    }
                    else
                    {
                        MessageBox.Show("error半径为负数");
                    }
                }
                else
                {
                    MessageBox.Show("输入为空");
                }
            }
            catch
            {
                MessageBox.Show("输入错误，请您检查您输入的内容。");
            }
        }
        //判断三角形是否合法
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                //判断三边是否完整
                if (str_1 != null && str_1.Length >= 6 && str_1.Contains('#') && (str_1.Contains('，') || str_1.Contains(',')))
                {
                    int i, j = 0;
                    // 遍历字符串，截取
                    for (i = 0; i < str_1.Length; i++)
                    {
                        //,间隔 #结束
                        if (','.Equals(str_1[i]) || '#'.Equals(str_1[i]) || '，'.Equals(str_1[i]))
                        {
                            //截取字符串，添加到list集合
                            list.Add(str_1.Substring(j, i - j));
                            j = i + 1;
                        }
                    }
                    //初始化三角形边长

                    double a = double.Parse(list[0]);
                    double b = double.Parse(list[1]);
                    double c = double.Parse(list[2]);

                    Triangle triangle = new Triangle(a, b, c);
                    //判断三角形是否合法
                    if (!triangle.isReasonable())
                    {
                        MessageBox.Show("不合法");
                        textBox1.Text = "";
                        list.Clear();//清空list 避免下次计算仍使用上次的值
                    }
                    else
                    {
                        MessageBox.Show("合法");
                    }
                }
                else
                {
                    MessageBox.Show("输入错误，或未完整输入");
                }
            }
            catch
            {
                MessageBox.Show("error");
                textBox1.Text = "";
                list.Clear();
            }
        }
        //点击计算三角形面积
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //判断三边是否完整
                if (str_1 != null && str_1.Length >= 6 && str_1.Contains('#') && (str_1.Contains('，') || str_1.Contains(',')))
                {
                    int i, j = 0;
                    // 遍历字符串，截取
                    for (i = 0; i < str_1.Length; i++)
                    {
                        //,间隔 #结束
                        if (','.Equals(str_1[i]) || '#'.Equals(str_1[i]) || '，'.Equals(str_1[i]))
                        {
                            //截取字符串，添加到list集合
                            list.Add(str_1.Substring(j, i - j));
                            j = i + 1;
                        }
                    }
                    //初始化三角形边长

                    double a = double.Parse(list[0]);
                    double b = double.Parse(list[1]);
                    double c = double.Parse(list[2]);
                    Triangle triangle = new Triangle(a, b, c);
                    //判断三角形是否合法
                    if (!triangle.isReasonable())
                    {
                        MessageBox.Show("三边未构成三角形");
                        //textBox1.Text = "";
                        list.Clear();//清空list 避免下次计算仍使用上次的值
                    }

                    double value = triangle.CalculatedArea();
                    MessageBox.Show("三角形面积：" + value.ToString());
                    //textBox1.Text = "";
                    list.Clear();
                }
                else
                {
                    MessageBox.Show("输入错误，或未完整输入");
                }
            }
            catch
            {
                MessageBox.Show("error");
                textBox1.Text = "";
                list.Clear();
            }
        }
        //判断正方形合法性
        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                double a = double.Parse(str_3);
                Square square = new Square(a);
                if (square.isReasonable())
                {
                    //合法
                    MessageBox.Show("合法");
                }
                else
                {
                    MessageBox.Show("不合法");
                }
            }
            catch
            {
                MessageBox.Show("输入错误，请您检查您输入的内容。");
            }
        }
        //计算正方形面积
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {

                double a = double.Parse(str_3);
                if (str_3 != null)
                {
                    if (a >= 0)
                    {
                        Square square = new Square(a);
                        MessageBox.Show("正方形的面积：" + square.CalculatedArea().ToString());
                    }
                    else
                    {
                        MessageBox.Show("error边长为负数");
                    }
                }
                else
                {
                    MessageBox.Show("输入为空");
                }
            }
            catch
            {
                MessageBox.Show("输入错误，请您检查您输入的内容。");
            }
        }
        //判断长方形合法性
        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                //判断边是否完整
                if (str_4 != null && str_4.Length >= 4 && str_4.Contains('#') && (str_4.Contains('，') || str_4.Contains(',')))
                {
                    int i, j = 0;
                    // 遍历字符串，截取
                    for (i = 0; i < str_4.Length; i++)
                    {
                        //,间隔 #结束
                        if (','.Equals(str_4[i]) || '#'.Equals(str_4[i]) || '，'.Equals(str_4[i]))
                        {
                            //截取字符串，添加到list集合
                            list.Add(str_4.Substring(j, i - j));
                            j = i + 1;
                        }
                    }
                    //初始化边长
                    double a = double.Parse(list[0]);
                    double b = double.Parse(list[1]);
                    Rectangle rectangle = new Rectangle(a, b);
                    if (!rectangle.isReasonable())
                    {
                        MessageBox.Show("不合法");
                        //textBox1.Text = "";
                        list.Clear();//清空list 避免下次计算仍使用上次的值
                    }
                    else
                    {
                        MessageBox.Show("合法");
                    }

                }
                else
                {
                    MessageBox.Show("输入错误，或未完整输入");
                }
            }
            catch
            {
                MessageBox.Show("error");
                textBox1.Text = "";
                list.Clear();
            }
        }
        //计算长方形面积
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                //判断边是否完整
                if (str_4 != null && str_4.Length >= 4 && str_4.Contains('#') && (str_4.Contains('，') || str_4.Contains(',')))
                {
                    int i, j = 0;
                    // 遍历字符串，截取
                    for (i = 0; i < str_4.Length; i++)
                    {
                        //,间隔 #结束
                        if (','.Equals(str_4[i]) || '#'.Equals(str_4[i]) || '，'.Equals(str_4[i]))
                        {
                            //截取字符串，添加到list集合
                            list.Add(str_4.Substring(j, i - j));
                            j = i + 1;
                        }
                    }
                    //初始化边长
                    double a = double.Parse(list[0]);
                    double b = double.Parse(list[1]);

                    Rectangle rectangle = new Rectangle(a, b);
                    double value = rectangle.CalculatedArea();
                    MessageBox.Show("长方形面积：" + value.ToString());
                    //textBox1.Text = "";
                    list.Clear();
                }
                else
                {
                    MessageBox.Show("输入错误，或未完整输入");
                }
            }
            catch
            {
                MessageBox.Show("error");
                textBox1.Text = "";
                list.Clear();
            }
        }

        //关闭窗口，返回目录
        private void button5_Click(object sender, EventArgs e)
        {
            Close();
        }
        //随机十个形状面积和
        private void button10_Click(object sender, EventArgs e)
        {
            Random random = new Random();

            double[] r = new double[7];//存随机数

            Triangle triangle = new Triangle();
            Rectangle rectangle = new Rectangle();
            Circular circular = new Circular();
            Square square = new Square();

            string text = "";//输出文本
            double total = 0;//总面积
            int i, j;
            //随机十个形状
            for (j = 0; j < 10; j++)
            {
                while (true)
                {
                    //随机边长
                    for (i = 0; i < 7; i++)
                    {
                        string s0 = random.Next().ToString();
                        //取0到10的随机数,随机数的后面两位，再除以10
                        r[i] = (double)((int)Convert.ToInt64(s0.Substring(s0.Length - 2, 2))) / 10;
                    }
                    triangle.setTength1(r[0]);
                    triangle.setTength2(r[1]);
                    triangle.setTength3(r[2]);
                    rectangle.seta(r[3]);
                    rectangle.setb(r[4]);
                    circular.setr(r[5]);
                    square.seta(r[6]);
                    //随机数让形状合法才进行计算面积
                    if (triangle.isReasonable() && rectangle.isReasonable() && circular.isReasonable() && square.isReasonable())
                    {
                        break;
                    }
                    i = 0;
                }

                string s1 = random.Next().ToString();
                double rand = (double)((int)Convert.ToInt64(s1.Substring(s1.Length - 2, 2))) / 10;

                if (0 < rand && rand <= 2.5)
                {
                    double triangleArea = triangle.CalculatedArea();
                    text += "三角形边长：" + r[0] + ", " + r[1] + ", " + r[2] + "   " + "面积：" + triangleArea + "\r\n";
                    total += triangleArea;
                }
                if (2.5 < rand && rand <= 5)
                {
                    double rectangleArea = rectangle.CalculatedArea();
                    text += "长方形边长：" + r[3] + ", " + r[4] + "   " + "面积：" + rectangleArea + "\r\n";
                    total += rectangleArea;
                }
                if (5 < rand && rand <= 7.5)
                {
                    double circularArea = circular.CalculatedArea();
                    text += "圆的半径：" + r[5] + "    " + "面积：" + circularArea + "\r\n";
                    total += circularArea;
                }
                if (7.5 < rand && rand <= 10)
                {
                    double squareArea = square.CalculatedArea();
                    text += "正方形边长：" + r[6] + "   " + "面积：" + squareArea + "\r\n";
                    total += squareArea;
                }
            }
            text += "面积之和：" + total + "\r\n";
            textBox5.Text = text;
        }
    }
}
